﻿@{
    ViewBag.Title = $"{GlobalContext.SystemConfig.SystemName} - Login";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment HostingEnvironment
@section header{
    <link href='@Url.Content("~/yisha/css/login.css")' rel="stylesheet" />
    <script>
        if (window.top !== window.self) { window.top.location = window.location };
    </script>
    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/jquery.validation/1.14.0/jquery.validate.min.js"))

    <style>
        .abcRioButton {
            width: 100% !important;
        }
    </style>
}
<div class="login-panel">
    <div class="row section">
        <div class="col-sm-12">
            <form id="form">
                <h4 class="no-margin">Login With Google Account...</h4>
                <br />
                <br />
                <div class="g-signin2" id="btnLoginByGoogle" data-onsuccess="onSignIn"></div>
                @*<button class="btn btn-success btn-block" onclick="signOut(); return false;">signOut</button>*@

            </form>
        </div>
    </div>

</div>
<script type="text/javascript">
    function onSignIn(googleUser) {
        console.log('Google Login callback');
        var profile = googleUser.getBasicProfile();
        console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
        console.log('Name: ' + profile.getName());
        console.log('Image URL: ' + profile.getImageUrl());
        console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
        console.log(googleUser);
        //这里得到了id，但是如何判断id不是恶意注入？
        //https://oauth2.googleapis.com/tokeninfo?id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6ImVlYTFiMWY0MjgwN2E4Y2MxMzZhMDNhM2MxNmQyOWRiODI5NmRhZjAiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiYXpwIjoiNDc5OTQ4MTQ4MzE2LXU2NXRxZjQ0MTd1cWNycG41ZGc1Y2VlaW5kanN1OTFoLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXVkIjoiNDc5OTQ4MTQ4MzE2LXU2NXRxZjQ0MTd1cWNycG41ZGc1Y2VlaW5kanN1OTFoLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTEyMzYwMDIxNTAzMDUwMjc1MDg2IiwiZW1haWwiOiJ3YW5nbmluZzA4MTE1QGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJhdF9oYXNoIjoiWjZNZ0l5ZnljR3dZRUZlUVBwRVFGUSIsIm5hbWUiOiJuaW5nIHdhbmciLCJwaWN0dXJlIjoiaHR0cHM6Ly9saDMuZ29vZ2xldXNlcmNvbnRlbnQuY29tL2EtL0FPaDE0R2o0dzdIcWtUS2JmZWtmREJkS2RvV1V0S0RTajZuWVEtVjk0d2RNPXM5Ni1jIiwiZ2l2ZW5fbmFtZSI6Im5pbmciLCJmYW1pbHlfbmFtZSI6IndhbmciLCJsb2NhbGUiOiJ6aC1DTiIsImlhdCI6MTYxMTgxMTE3NywiZXhwIjoxNjExODE0Nzc3LCJqdGkiOiI0OWI2MGQyMTkzYWM5M2JlNGI2MDA0YTgzZWIzMTdkNGQxMDliZDMyIn0.l5ca1lggpgX9eU-GuCjz-SVNUSY-tFqPigQVlmp76oY7NMpPtRu427-AX3dDYtOPfGD87VYTrbMUbUVqt8JOz3FUAmjBgwb8YN4J8irDQ-XnfOwceZqjvVes4TwDLHY8FA6Kk_q5H77iv4Q4VwifW3S5AVNqATlFjYg-7r1E_aU5mijXY9cH-QXj4Q2qNejWlZ_Lq6Py9l5kQ4dVG8O3YKWV9KCofTRUAbbrEyP1qMU_kAk3pqi8aNoVPvTq4YmUUKdPhK9TS5o_nD9g-4IGEZcNpsVfatxefpIfVLx9Kz4F2-EHVFyVezo5-FRcfiz_pKl-cv8CChNUCE_hSQbYYA
        //调用这个地址，就可以得到验证

        var postData = { OpenID: profile.getId(), Email: profile.getEmail(), ImageUrl: profile.getImageUrl(), Name: profile.getName(), Provider: 'google'};
            ys.ajax({
                url: '@Url.Content("~/OAuth/GoogleLoginCallback")',
                type: "post",
                data: postData,
                dataType:"json",
                success: function (obj) {
                    if (obj.Tag == 1) {
                        ys.msgSuccess(obj.Message);
                        redirectToIndex();
                    }
                    else {
                        ys.msgError(obj.Message);
                    }
                }
            });
    }
    function redirectToIndex() {
        location.href = '@Url.Content("~/Home/Index")';
    }
    function signOut() {
        //只有这样，才能达到切换账号的效果，否则一个google账号，你永远也无法切换测试了
        var auth2 = gapi.auth2.getAuthInstance();
        auth2.signOut().then(function () {
            console.log('User signed out.');
            auth2.disconnect();
            redirectToIndex();
        });
        auth2.disconnect();
        setTimeout(() => { redirectToIndex(); }, 3000);
    }
    $(document).ready(function () {
        var loginTimer = setInterval(function () {
            var abcRioButtonCount = $("#btnLoginByGoogle .abcRioButton").length;
            console.log(abcRioButtonCount);
            if (abcRioButtonCount > 0) {
                clearInterval(loginTimer);
                $("#btnLoginByGoogle .abcRioButton").trigger("click");
                console.log("fire abcRioButton");
            }
        }, 100);

    });
</script>